package com.wing.sys.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wing.common.CommonSpringJDBC;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Code;

@Repository
public class CodeDao extends CommonSpringJDBC<Code, String> {

	public Code findByCode(String code) {
		String sql = "select t.* from sys_code t where t.code=?  ";
		List<Map<String, Object>> list = getSimpleJdbcTemplate().queryForList(sql,code);
		if(list.size()>0){
			Map map = (Map)list.get(0);
			Code c = new Code();
			c.setCode((String)map.get("code"));
			c.setId((String)map.get("id"));
			c.setName((String)map.get("name"));
			return c;
		}
		return null;
	}
	
	public List<Map<String, Object>> findChildByID(String id) {
		String sql = "select t.* from sys_code t where t.parent_id=? order by order_num";
		return super.getSimpleJdbcTemplate().queryForList(sql,id);
	}

	public List<Map<String, Object>> findChildByCode(String code) {
		String sql = "select t.* from sys_code t,sys_code t1 where t.parent_id=t1.id and t1.code=? order by t.order_num";
		return super.getSimpleJdbcTemplate().queryForList(sql,code);
	}
	
	public PageBean findPageList(int currentPage, int pageSize, String codeId) {
		String where= " parent_id='"+codeId+"'  ";
		String sql1="SELECT * " +
				" FROM sys_code where "+where+" ORDER BY order_num ASC,id ASC " +
				" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(id) from sys_code  where"+where;
		
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}
	
	public List findCodesByCode(String code) {
		String sql = "select * from sys_code t where code = ? and (valid_flag is null or valid_flag != -1)";
		return super.getSimpleJdbcTemplate().queryForList(sql,code);
	}
}
